//Test case for encoder/decoder:
//string schedule_encoder(Schedule* schedule);
//Schedule* schedule_decoder(string textSchedule);

TEST(Storage_test, encoder_decoder)
{
	remove("storage_test.txt");
	Storage timetable("storage_test.txt");
	string str=
		"122509"+SPILT_PARAMETER+"1900"+SPILT_PARAMETER
		+"2200"+SPILT_PARAMETER+"Aikido"+SPILT_PARAMETER
		+"mpsh"+SPILT_PARAMETER+"**"+SPILT_PARAMETER;

	Schedule* sch=timetable.schedule_decoder(str);
	
	string de_encoded_str = timetable.schedule_encoder(sch);
	
	ASSERT_EQ(str,de_encoded_str);
	remove("storage_test.txt");
}

//add line and remove line function testing
TEST(Storage_test, add_remove_schedule_test)
{
	remove("storage_test.txt");
	Storage timetable("storage_test.txt");

	Schedule* sch1=new Schedule("120925","1430","1715","coding","Embedded",Schedule::LOW);
	Schedule* sch2=new Schedule("120925","1730","1900","eating","SOC",Schedule::LOW);
	//expected string in storage_text
	string expected_text;
	expected_text+=timetable.schedule_encoder(sch1)+"|||";
	expected_text+=timetable.schedule_encoder(sch2)+"|||";
	
	//FUNCTION TESTING
	//------------------------------------
	//add functions
	timetable.add_schedule(sch1);
	timetable.add_schedule(sch1);
	timetable.add_schedule(sch2);
	//remove the 1st schedule in the file
	timetable.remove_schedule(1);
	//------------------------------------

	//go to storage_test and get all the string to compare
	fileManager mytext("Storage_test.txt");
	vector<string> sch_list = mytext.get_all_line();
	string text_file;
	for(unsigned i=0;i<sch_list.size();i++)
	{
		text_file+=sch_list[i]+"|||";
	}

	remove("storage_test.txt");

	ASSERT_EQ(expected_text,text_file);
}

//check find_schedule_index(Schedule*)
//case 1: can find (in the middle of the list)
TEST(Storage_test_find_index, test1_found)
{
	remove("storage_test.txt");
	Storage timetable("storage_test.txt");

	Schedule* sch1=new Schedule("120925","1430","1715","coding","Embedded",Schedule::LOW);
	Schedule* sch2=new Schedule("120925","1730","1900","eating","SOC",Schedule::HIGH);
	Schedule* sch3=new Schedule("120925","1430","1715","coding2","Embedded2",Schedule::NORMAL);
	Schedule* sch4=new Schedule("120925","1730","1900","eating2","SOC2",Schedule::LOW);

	timetable.add_schedule(sch1);
	timetable.add_schedule(sch2);
	timetable.add_schedule(sch3);
	timetable.add_schedule(sch4);

	Schedule* finding_schedule=new Schedule("120925","1730","1900","eating","SOC",Schedule::HIGH);
	int i=timetable.find_schedule_index(finding_schedule);

	remove("storage_test.txt");
	ASSERT_EQ(1,i);
}
//case 2: can't find (due to different in LEVEL of important)!
TEST(Storage_test_find_index, test2_cant_found)
{
	remove("storage_test.txt");
	Storage timetable("storage_test.txt");

	Schedule* sch1=new Schedule("120925","1430","1715","coding","Embedded",Schedule::LOW);
	Schedule* sch2=new Schedule("120925","1730","1900","eating","SOC",Schedule::HIGH);
	Schedule* sch3=new Schedule("120925","1430","1715","coding2","Embedded2",Schedule::NORMAL);
	Schedule* sch4=new Schedule("120925","1730","1900","eating2","SOC2",Schedule::LOW);

	timetable.add_schedule(sch1);
	timetable.add_schedule(sch2);
	timetable.add_schedule(sch3);
	timetable.add_schedule(sch4);

	Schedule* finding_schedule=new Schedule("120925","1730","1900","eating","SOC",Schedule::NORMAL);
	int i=timetable.find_schedule_index(finding_schedule);

	remove("storage_test.txt");
	ASSERT_EQ(-1,i);
}


//check schedue_at_index(int index)
TEST(Storage_test_sch_at_index, test1)
{
	remove("storage_test.txt");
	Storage timetable("storage_test.txt");

	Schedule* sch1=new Schedule("120925","1430","1715","coding","Embedded",Schedule::LOW);
	Schedule* sch2=new Schedule("120925","1730","1900","eating","SOC",Schedule::HIGH);
	Schedule* sch3=new Schedule("120925","1430","1715","coding2","Embedded2",Schedule::NORMAL);
	Schedule* sch4=new Schedule("120925","1730","1900","eating2","SOC2",Schedule::LOW);

	timetable.add_schedule(sch1);
	timetable.add_schedule(sch2);
	timetable.add_schedule(sch3);
	timetable.add_schedule(sch4);

	Schedule* output_schedule=timetable.schedule_at_index(0);
	remove("storage_test.txt");

	ASSERT_EQ(timetable.schedule_encoder(sch1),
		timetable.schedule_encoder(output_schedule));
}

TEST(Storage_test_search_keyword,test1)
{
	Storage timetable("storage_test.txt");

	Schedule* sch1=new Schedule("120925","1430","1715","coding","Embedded",Schedule::LOW);
	Schedule* sch2=new Schedule("120925","1730","1900","eating","SOC",Schedule::HIGH);
	Schedule* sch3=new Schedule("120926","1430","1715","codIng2","EmbEdded2",Schedule::NORMAL);
	Schedule* sch4=new Schedule("120926","1730","1900","eating2","SOC2",Schedule::LOW);
	Schedule* sch5=new Schedule("120927","1430","1715","coding3","Embedded3",Schedule::LOW);
	Schedule* sch6=new Schedule("120927","1730","1900","eating3","SOC3",Schedule::HIGH);
	Schedule* sch7=new Schedule("120928","1430","1715","codIng4","EmbEdded4",Schedule::NORMAL);
	Schedule* sch8=new Schedule("120928","1730","1900","eating4","SOC4",Schedule::LOW);

	timetable.add_schedule(sch1);
	timetable.add_schedule(sch2);
	timetable.add_schedule(sch3);
	timetable.add_schedule(sch4);
	timetable.add_schedule(sch5);
	timetable.add_schedule(sch6);
	timetable.add_schedule(sch7);
	timetable.add_schedule(sch8);

	//vector<Schedule*> searchList= timetable.search_schedule("embeDded",Schedule::PLACE);

	vector<Schedule*> searchList= timetable.search_schedule("cOdInG",Schedule::NAME);
	string output;
	for(int i=0;i<searchList.size();i++)
	{
		output+=timetable.schedule_encoder(searchList[i]);
	}

	string expected;
	expected+=timetable.schedule_encoder(sch1);
	expected+=timetable.schedule_encoder(sch3);
	expected+=timetable.schedule_encoder(sch5);
	expected+=timetable.schedule_encoder(sch7);
	remove("storage_test.txt");

	ASSERT_EQ(output,expected);
}

TEST(Storage_test_search_keyword,test2)
{
	Storage timetable("storage_test.txt");

	Schedule* sch1=new Schedule("120925","1430","1715","coding","Embedded",Schedule::LOW);
	Schedule* sch2=new Schedule("120925","1730","1900","eating","SOC",Schedule::HIGH);
	Schedule* sch3=new Schedule("120926","1430","1715","codIng2","EmbEdded2",Schedule::NORMAL);
	Schedule* sch4=new Schedule("120926","1730","1900","eating2","SOC2",Schedule::LOW);
	Schedule* sch5=new Schedule("120927","1430","1715","coding3","Embedded3",Schedule::LOW);
	Schedule* sch6=new Schedule("120927","1730","1900","eating3","SOC3",Schedule::HIGH);
	Schedule* sch7=new Schedule("120928","1430","1715","codIng4","EmbEdded4",Schedule::NORMAL);
	Schedule* sch8=new Schedule("120928","1730","1900","eating4","SOC4",Schedule::LOW);

	timetable.add_schedule(sch1);
	timetable.add_schedule(sch2);
	timetable.add_schedule(sch3);
	timetable.add_schedule(sch4);
	timetable.add_schedule(sch5);
	timetable.add_schedule(sch6);
	timetable.add_schedule(sch7);
	timetable.add_schedule(sch8);

	vector<Schedule*> searchList= timetable.search_schedule("eating4",Schedule::NAME);

	string output;
	for(int i=0;i<searchList.size();i++)
	{
		output+=timetable.schedule_encoder(searchList[i]);
	}

	string expected;
	expected+=timetable.schedule_encoder(sch8);
	remove("storage_test.txt");

	ASSERT_EQ(output,expected);
}